package cn.jinjimi.app.presenter;

import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.controller.CustomerController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.LoginSuccessEvent;
import cn.jinjimi.app.controller.event.TodayRemindEvent;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.entity.Insurance;
import cn.jinjimi.app.custom.entity.RemindBean;
import cn.jinjimi.app.custom.entity.TodayRemind;
import cn.jinjimi.app.custom.event.CustomChangeEvent;
import cn.jinjimi.app.custom.event.InsureChangeEvent;
import cn.jinjimi.app.custom.iview.CustomRemindView;
import cn.jinjimi.app.entity.Holiday;
import cn.jinjimi.app.interfaces.IView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 主页-客户-提醒
 * <p/>
 * Created by jinsen on 15/11/19.
 */
public class RemindPresenter extends NetBasePresenter {

    CustomRemindView mView;

    public RemindPresenter(IView view) {
        super(view);
        mView = (CustomRemindView) view;
    }

    public void requestData() {
        if (AccountManager.getInstance().isLogin())
            CustomerController.getTodayRemind();
    }

    public void refresh() {
        requestData();
    }

    /**
     * 提醒数据的变化反应
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTodayRemindEvent(TodayRemindEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                mView.loadSucc();
                String data = event.data;
                try {
                    JSONObject json = new JSONObject(data);
                    TodayRemind mTodayRemindData = GsonUtils.fromJson(json.optString("remind_list"), TodayRemind.class);
                    List<RemindBean> remindList = new ArrayList<RemindBean>();
                    if (mTodayRemindData.getBirthday_customers() != null) {
                        for (Custom c : mTodayRemindData.getBirthday_customers()) {
                            RemindBean cRemind = new RemindBean();
                            cRemind.type = RemindBean.TYPE_CUSTOMER;
                            cRemind.difference = c.difference;
                            cRemind.data = c;
                            remindList.add(cRemind);
                        }
                    }
                    if (mTodayRemindData.getPolicies() != null) {
                        for (Insurance insurance : mTodayRemindData.getPolicies()) {
                            RemindBean iRemindBean = new RemindBean();
                            iRemindBean.type = RemindBean.TYPE_INSURE;
                            iRemindBean.difference = insurance.getDifference();
                            iRemindBean.data = insurance;
                            remindList.add(iRemindBean);
                        }
                    }

                    if (mTodayRemindData.getHolidays() != null) {
                        for (Holiday h : mTodayRemindData.getHolidays()) {
                            RemindBean hRemind = new RemindBean();
                            hRemind.type = RemindBean.TYPE_HOLIDAY;
                            hRemind.difference = h.getDifference();
                            hRemind.data = h;
                            remindList.add(hRemind);
                        }
                    }
                    Collections.sort(remindList);
                    addRemindListTitle(remindList);
                    mView.setData(remindList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case DataEvent.FAIL:
                mView.loadFail();
                break;
        }
    }

    /**
     * 添加提醒数据
     * 分类栏
     *
     * @param remindList
     */
    public synchronized void addRemindListTitle(List<RemindBean> remindList) {
        int titleCount = -1;
        for (int i = 0; i < remindList.size(); i++) {
            RemindBean bean = remindList.get(i);
            if (bean.difference == 0 && titleCount < 0) {
                RemindBean today = new RemindBean();
                today.type = RemindBean.TYPE_TITLE;
                today.data = "今天";
                remindList.add(0, today);
                titleCount = 0;
                i++;
            } else if (bean.difference > 0 && bean.difference < 8 && titleCount < 7) {
                RemindBean week = new RemindBean();
                week.type = RemindBean.TYPE_TITLE;
                week.data = "7天内";
                remindList.add(i, week);
                titleCount = 7;
                i++;
            } else if (bean.difference > 7 && titleCount < 30) {
                RemindBean month = new RemindBean();
                month.type = RemindBean.TYPE_TITLE;
                month.data = "30天内";
                remindList.add(i, month);
                titleCount = 30;
                i++;
            }
        }

    }

    /**
     * 客户资料发生更改
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomChangeEvent(CustomChangeEvent event) {
        refresh();
    }

    /**
     * 客户保单发生更改
     * 新增
     * 删除
     * 修改
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onInsureChangeEvent(InsureChangeEvent event) {
        refresh();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginSuccessEvent(LoginSuccessEvent event) {
        if (!AccountManager.getInstance().isLogin()) {
            mView.loading();
        } else {
            refresh();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mView = null;
    }
}
